package com.k3cloud.rpc.core;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.k3cloud.base.dto.entity.ExecutionTypeDO;
import com.k3cloud.base.type.ClassificationTypeEnum;
import com.k3cloud.base.type.OutJsonTypeEnum;
import com.k3cloud.base.work.WorkInterfaceType;
import com.k3cloud.base.work.WorkRoutineServer;
import com.k3cloud.rpc.factory.RpcDataRealizationFactory;
import com.k3cloud.rpc.http.RemoteHttpClient;
import com.k3cloud.rpc.service.RpcSettingService;
import com.ty.basic.exception.message.SystemErrorCodeEnum;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class SendHttpServer {
    public JSONObject sendUserDataToServer(String code, String name, OutJsonTypeEnum outJsonTypeEnum,
                                           ClassificationTypeEnum classificationTypeEnum, FlowObject flowObject, Object parameter){
        RpcSettingService rpcSettingService=SpringUtil.getBean(RpcSettingService.class);
        RemoteHttpClient remoteHttpClient=SpringUtil.getBean(RemoteHttpClient.class);
        RpcDataRealizationFactory rpcDataRealizationFactory=SpringUtil.getBean(RpcDataRealizationFactory.class);

        WorkRoutineServer workRoutineServer=null;
        Map<String,WorkRoutineServer> WorkRoutineServerMap= SpringUtil.getBeansOfType(WorkRoutineServer.class);
        if(WorkRoutineServerMap!=null){
            for(Map.Entry<String,WorkRoutineServer> entity: WorkRoutineServerMap.entrySet()){
                WorkInterfaceType workInterfaceType=entity.getValue().getClass().getAnnotation(WorkInterfaceType.class);
                if(workInterfaceType.typeValue().equals(outJsonTypeEnum.value())
                && workInterfaceType.classificationValue().equals(classificationTypeEnum.value())){
                    workRoutineServer=entity.getValue();
                    break;
                }
            }
        }
        if(workRoutineServer==null){
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("没有【"+outJsonTypeEnum.value()+","+classificationTypeEnum.value()+"】处理实例");
        }
        Map<String,Object> outMap=new HashMap<>();
        try {
            ExecutionTypeDO executionType=rpcDataRealizationFactory.getExecutionType(code, name);
            outMap=workRoutineServer.productionCall(executionType,code,name,flowObject,parameter);
            JSONObject result=remoteHttpClient.RemoteHttpToServer(outJsonTypeEnum,classificationTypeEnum,null,executionType,outMap,flowObject,parameter);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            rpcSettingService.saveLogs(UUID.randomUUID().toString(),JSONObject.toJSONString(outMap),"{}",name,code,e.getMessage());
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(e.getMessage());
        }
    }
    public JSONObject sendUserDataToChangeServer(String code, String name, OutJsonTypeEnum outJsonTypeEnum,
                                           ClassificationTypeEnum classificationTypeEnum, FlowObject flowObject,Object parameter){
        RpcSettingService rpcSettingService=SpringUtil.getBean(RpcSettingService.class);
        RemoteHttpClient remoteHttpClient=SpringUtil.getBean(RemoteHttpClient.class);
        RpcDataRealizationFactory rpcDataRealizationFactory=SpringUtil.getBean(RpcDataRealizationFactory.class);

        WorkRoutineServer workRoutineServer=null;
        Map<String,WorkRoutineServer> WorkRoutineServerMap= SpringUtil.getBeansOfType(WorkRoutineServer.class);
        if(WorkRoutineServerMap!=null){
            for(Map.Entry<String,WorkRoutineServer> entity: WorkRoutineServerMap.entrySet()){
                WorkInterfaceType workInterfaceType=entity.getValue().getClass().getAnnotation(WorkInterfaceType.class);
                if(workInterfaceType.typeValue().equals(outJsonTypeEnum.value())
                        && workInterfaceType.classificationValue().equals(classificationTypeEnum.value())){
                    workRoutineServer=entity.getValue();
                    break;
                }
            }
        }
        if(workRoutineServer==null){
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException("没有【"+outJsonTypeEnum.value()+","+classificationTypeEnum.value()+"】处理实例");
        }
        Map<String,Object> outMap=new HashMap<>();
        try {
            ExecutionTypeDO executionType=rpcDataRealizationFactory.getExecutionType(code, name);
            outMap=workRoutineServer.productionCall(executionType,code,name,flowObject,parameter);
            JSONObject result=remoteHttpClient.RemoteHttpChangeToServer(outJsonTypeEnum,classificationTypeEnum,null,executionType,outMap,flowObject,parameter);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            rpcSettingService.saveLogs(UUID.randomUUID().toString(),JSONObject.toJSONString(outMap),"{}",name,code,e.getMessage());
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(e.getMessage());
        }
    }
    public JSONObject selectUserDataToServer(String code, String name, OutJsonTypeEnum outJsonTypeEnum,
                                           ClassificationTypeEnum classificationTypeEnum,List<String> phone){
        RpcSettingService rpcSettingService=SpringUtil.getBean(RpcSettingService.class);
        RemoteHttpClient remoteHttpClient=SpringUtil.getBean(RemoteHttpClient.class);
        RpcDataRealizationFactory rpcDataRealizationFactory=SpringUtil.getBean(RpcDataRealizationFactory.class);


        Map<String,Object> outMap=new HashMap<>();
        try {
            outMap.put("mobiles",phone);
            ExecutionTypeDO executionType=rpcDataRealizationFactory.getExecutionType(code, name);
            executionType.setUri("/open-apis/contact/v3/users/batch_get_id?user_id_type=user_id");
            JSONObject result=remoteHttpClient.selectUserDataToServer(outJsonTypeEnum,classificationTypeEnum,null,executionType,outMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            rpcSettingService.saveLogs(UUID.randomUUID().toString(),JSONObject.toJSONString(outMap),"{}",name,code,e.getMessage());
            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(e.getMessage());
        }
    }
    public JSONArray getUserIdDataToServer(String code, String name, OutJsonTypeEnum outJsonTypeEnum,
                                           ClassificationTypeEnum classificationTypeEnum,List<String> phone){
        JSONObject resultHttp=selectUserDataToServer(code,name,outJsonTypeEnum,classificationTypeEnum,phone);
        if(resultHttp.getLongValue("code")==0){
            return resultHttp.getJSONObject("data").getJSONArray("user_list");
        }
        return null;
    }
}
